<?php

namespace app\common\model\Order;

use think\Model;
use think\Request;

// 订单
class Order extends Model
{
    protected $name = 'order';

    protected $autoWriteTimestamp = true;
    protected $updateTime = false;
    protected $createTime = 'time';

    // 时间段表
    public function timelist()
    {
        return $this->belongsTo('app\common\model\Airline\Pssd', 'ps_time_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
    // 航线表
    public function airline()
    {
        return $this->belongsTo('app\common\model\Airline\Airline', 'airline_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
    // 起点
    public function startPoint()
    {
        return $this->belongsTo('app\common\model\Airline\Points', 'start_point_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
    // 终点
    public function endPoint()
    {
        return $this->belongsTo('app\common\model\Airline\Points', 'end_point_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
    // 物品类型
    public function type()
    {
        return $this->belongsTo('app\common\model\Product\Type', 'item_type_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
    // 物品单位
    public function unit()
    {
        return $this->belongsTo('app\common\model\Product\Unit', 'item_unit_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
    // 无人机信息
    public function uav()
    {
        return $this->belongsTo('app\common\model\Airline\Uav', 'uav_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }



    protected $append = [
        'status_text',  //订单状态
        'image_text',  //分类图片
        'note_text',  //下单时间
        'phone_text',  //联系人手机号
        'ysyq_text',  //运输要求
    ];

    public function getStatusTextAttr($value, $data)
    {
        $list = ['1' => '待配送', '2' => '配送中', '3' => '已完成', '4' => '已取消', '5' => '审核中', '6' => '已拒绝'];

        $status = !empty($data['status']) ? $list[$data['status']] : '';

        return $status;
    }

    public function getImageTextAttr($value, $data)
    {
        // 获取当前域名
        $url = Request::instance()->domain();
        $image = !empty($data['image']) ? $url.$data['image'] :  $url.'/assets/img/image.png';

        return $image;
    }

    public function getNoteTextAttr($value, $data)
    {
        $note = !empty($data['note']) ? $data['note'] :  '无';

        return $note;
    }

    public function getPhoneTextAttr($value, $data)
    {
        $phone = !empty($data['contact_phone']) ? $data['contact_phone'] :  '';
        $phone=substr_replace($phone, '****', 3,4);

        return $phone;

    }

    public function getYsyqTextAttr($value, $data)
    {
        $list = ['0' => '常规运输', '1' => '温控运输'];

        $ysyq = !empty($list[$data['ys_request']]) ? $list[$data['ys_request']] : '';

        return $ysyq;
    }
}
